package com.nannybear.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ProgressBar;

import com.nannybear.R;

import java.util.ArrayList;

/**
 * @brief add data and draw the timeline
 */
public class Timeline extends ProgressBar {
    /********** DECLARATIONS **********/
    private ArrayList<ProgressItem> mProgressItemsList;
    private float totalSpan = 1500;
    private int spanSize = 75;
    private int totalProgressItemWidth;

    /************ METHODS ************/
    /**
     * @brief constructor
     * @param context
     */
    public Timeline(Context context) {
        super(context);
        this.initDataToSeekbar();
    }

    /**
     * @brief constructor
     * @param context
     * @param attrs : collection of attributes of XML
     */
    public Timeline(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initDataToSeekbar();
    }

    /**
     * @brief constructor
     * @param context
     * @param attrs : collection of attributes of XML
     * @param defStyle : style of the timeline
     */
    public Timeline(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.initDataToSeekbar();
    }

    /**
     * @brief measure the view
     */
    @Override
    protected synchronized void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * @brief draw the timeline
     * @param canvas : the canvas on which the background will be drawn
     */
    protected void onDraw(Canvas canvas) {
        totalProgressItemWidth = 0;
        if (mProgressItemsList.size() > 0) {
            int progressBarWidth = getWidth();

            int progressBarHeight = getHeight();
            int lastProgressX = 0;
            int progressItemWidth, progressItemRight;

            for (int i = 0; i < mProgressItemsList.size(); i++) {
                ProgressItem progressItem = mProgressItemsList.get(i);
                Paint progressPaint = new Paint();
                progressPaint.setColor(getResources().getColor(progressItem.color));

                progressItemWidth = (int) (progressItem.progressItemPercentage * progressBarWidth / 100);
                totalProgressItemWidth = progressItemWidth*(mProgressItemsList.size()-1);
                progressItemRight = lastProgressX + progressItemWidth;

                // for last item give right to progress item to the width
                if ((i == mProgressItemsList.size() - 1) && (progressItemRight != progressBarWidth)) {
                    progressItemRight = progressBarWidth;
                }
                RectF progressRect = new RectF();

                progressRect.set(lastProgressX, 0, progressItemRight, progressBarHeight);
                canvas.drawRoundRect(progressRect, 8, 8, progressPaint);
                lastProgressX = progressItemRight;
            }
            super.onDraw(canvas);
        }
    }

    /**
     * @brief add data in arrayList
     */
    private void initDataToSeekbar(){
        mProgressItemsList = new ArrayList<ProgressItem>();
        ProgressItem mProgressItem;

        mProgressItem = new ProgressItem();
        mProgressItem.color = R.color.grey_wallpaper;
        mProgressItemsList.add(mProgressItem);
        this.invalidate();
    }

    /**
     * @brief zoom in and zoom out the timeline
     */
    public void zoomTimeline(float zoom){
        ProgressItem mProgressItem;
        for (int i = 0; i < mProgressItemsList.size(); i++) {
            mProgressItem = mProgressItemsList.get(i);
            mProgressItem.progressItemPercentage = ((spanSize * zoom) / totalSpan) * 100;
            if(getTotalProgressItemWidth() >= getWidth()){
                Log.i("Tl", "out");
            }
            else{

            }
        }
        this.invalidate();
    }

    /**
     * @brief add a rectangle in the timeline
     * @param newSpanColor : color of the rectangle
     */
    public void addARect(int newSpanColor){
        ProgressItem mProgressItem;
        mProgressItemsList.remove(mProgressItemsList.size() - 1);

        // blue span
        mProgressItem = new ProgressItem();
        mProgressItem.progressItemPercentage = (spanSize / totalSpan) * 100;
        mProgressItem.color = newSpanColor;
        mProgressItemsList.add(mProgressItem);

        // grey span
        mProgressItem = new ProgressItem();
        mProgressItem.color = R.color.grey_wallpaper;
        mProgressItemsList.add(mProgressItem);

        this.invalidate();
    }

    /*********** ACCESSORS ***********/
    public float getTotalSpan() {
        return totalSpan;
    }

    public void setTotalSpan(float totalSpan) {
        this.totalSpan = totalSpan;
    }

    public ArrayList<ProgressItem> getmProgressItemsList() {
        return mProgressItemsList;
    }

    public void setmProgressItemsList(ArrayList<ProgressItem> mProgressItemsList) {
        this.mProgressItemsList = mProgressItemsList;
    }

    public int getTotalProgressItemWidth() {
        return totalProgressItemWidth;
    }
}